package com.isoft.mapper;

import com.isoft.entity.DfmeCklsEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeCklsMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "msgId", keyColumn = "MSG_ID")
    void insertMeta(DfmeCklsEntity entity);

    @Insert("INSERT INTO FLIGHT_BASE(" +
            "FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR})")
    int insertFlightBase(DfmeCklsEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO CHECKIN_COUNTER(COUNTER_ID, FLIGHT_ID, COUNTER_NUMBER, START_TIME, END_TIME, COUNTER_TYPE) ",
            "VALUES ",
            "<if test='dflt.ckls.fces != null'>",
            "(SEQ_COUNTER.nextval, #{dflt.flightId}, #{dflt.ckls.fces}, ",
            "TO_TIMESTAMP(#{dflt.ckls.fces,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{dflt.ckls.fcee,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), 'F'),",
            "</if>",
            "<if test='dflt.ckls.mces != null'>",
            "(SEQ_COUNTER.nextval, #{dflt.flightId}, #{dflt.ckls.mces}, ",
            "TO_TIMESTAMP(#{dflt.ckls.mces,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{dflt.ckls.mcee,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), 'M')",
            "</if>",
            "</script>"
    })
    int insertCheckinCounter(DfmeCklsEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO CHECKIN_COUNTER(COUNTER_ID, FLIGHT_ID, COUNTER_NUMBER, START_TIME, END_TIME, COUNTER_TYPE) ",
            "VALUES ",
            "<foreach collection='dflt.ckls.counters' item='counter' separator=','>",
            "(SEQ_COUNTER.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{counter.code,jdbcType=VARCHAR}, ",
            "TO_TIMESTAMP(#{counter.estimatedStartTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{counter.estimatedEndTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "'F')",
            "</foreach>",
            "</script>"
    })
    int insertCounters(DfmeCklsEntity entity);
}